﻿package pw.archipelag.loaderGame.panel.scores {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	
	import flash.utils.Timer;
	import base.tf.TextFieldUtils;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinTf;
	import base.dspObj.DoOperations;
	import base.tf.TextFieldUtilsDots;
	import flash.text.TextFormat;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import com.greensock.TweenNano;
	
	public class MessageScoreOperation extends Sprite {
		
		private var tfMessage: TextField;
		private var timerCloseMessage: Timer;
		private var isMessageScoreOrLoadSave: uint = 0;
		
		public function MessageScoreOperation(): void {
			this.alpha = 0;
			this.addTfMessage();
		}

		private function addTfMessage(): void {
			this.tfMessage = TextFieldUtils.createTextField((new SkinTf()).tFormat, 0, 1);
			this.tfMessage.width = 300;
            this.addChild(this.tfMessage);
        }
		
		private function getStrPoints(scorePoints: uint): String {
			var strPoints: String;
			if ((Math.abs(scorePoints) == 0) || ((Math.abs(scorePoints) >= 5) && (Math.abs(scorePoints) < 20)) 
			 || ((Math.abs(scorePoints) >= 20) && ((Math.abs(scorePoints) % 10 == 0) || (Math.abs(scorePoints) % 10 == 1) || (Math.abs(scorePoints) % 10 == 5) || (Math.abs(scorePoints) % 10 == 6) || (Math.abs(scorePoints % 10) == 7) || (Math.abs(scorePoints) % 10 == 8) || (Math.abs(scorePoints) % 10 == 9))))
				strPoints = "punktów";
			else if (Math.abs(scorePoints) == 1)
				strPoints = "punkt";
			else if ((Math.abs(scorePoints) == 2) || (Math.abs(scorePoints) == 3) || (Math.abs(scorePoints) == 4)
					|| ((Math.abs(scorePoints) >= 20) && ((Math.abs(scorePoints) % 10 == 2) || (Math.abs(scorePoints) % 10 == 3) || (Math.abs(scorePoints) % 10 == 4))))
				strPoints = "punkty";
			return strPoints;
		}
		
		private function initShowAndCloseMessage(): void {
			TextFieldUtilsDots.stopAddDots(this.tfMessage);
			this.tfMessage.text = "";
		}

		private function setMessageForScoreOrLoadSave(isMessageScoreOrLoadSave: uint): void {
			this.isMessageScoreOrLoadSave = isMessageScoreOrLoadSave;
			var tFormatMessage: TextFormat = this.tfMessage.getTextFormat();
			tFormatMessage.align = [TextFormatAlign.CENTER, TextFormatAlign.LEFT][isMessageScoreOrLoadSave];
			this.tfMessage.setTextFormat(tFormatMessage);
			this.tfMessage.defaultTextFormat = tFormatMessage;
			DoOperations.hideShow(this, 1);
		}
		
		public function showAndCloseScoreMessage(scorePoints: uint, scorePercent: uint): void {
			this.initShowAndCloseMessage();
			this.setMessageForScoreOrLoadSave(0);
			this.tfMessage.text = "Uzyskałeś " + String(scorePoints) + " " + this.getStrPoints(scorePoints) + ".\nTwoja wiedza jest na poziomie " + String(scorePercent) + "%.";
			this.showAndCloseMessage(3000);
		}

		public function showDurationMessage(isSaveLoad: uint = 0): void {
			this.setMessageForScoreOrLoadSave(1);
			this.tfMessage.text = ["Zapisywanie wyników", "Wczytywanie wyników"][isSaveLoad];
			TextFieldUtilsDots.addDots(this.tfMessage);
		}

		public function showAndCloseLoadSaveMessage(typeMessageLoad: uint = 1, typeMessageSave: int = -1): void {
			this.initShowAndCloseMessage();
			var time: Number = 3000;
			var isShowMessageLoad: Boolean = Boolean((typeMessageLoad == 0) || ((typeMessageLoad == 1) && (typeMessageSave == -1)));
			if (typeMessageSave > -1) this.tfMessage.text = ["Twój wynik został po raz pierwszy zapisany na liście wyników.", "Uzyskałeś swój najlepszy wynik i został on zapisany.", "Uzyskałeś gorszy wynik od swojego najlepszego i nie został on zapisany.", "Błąd zapisu wyniku.", "Nie jesteś zalogowany i twój wynik nie został zapisany."][typeMessageSave] + ["", "\n"][uint(isShowMessageLoad)];
			if (isShowMessageLoad) this.tfMessage.appendText(["Błąd wczytywania wyników.", "Pomyślnie wczytano wyniki."][typeMessageLoad]);
			if (typeMessageSave > -1) time += 1500;
			this.showAndCloseMessage(time);
		}
		
		private function showAndCloseMessage(time: uint): void {
			this.timerCloseMessage = new Timer(time, 1);
			this.timerCloseMessage.addEventListener(TimerEvent.TIMER_COMPLETE, this.onTimeToCloseMessageRunOut);
			this.timerCloseMessage.start();
		}
		
		private function onTimeToCloseMessageRunOut(e: TimerEvent): void {
			DoOperations.hideShow(this, 0, 10, this.close);
		}
		
		private function close(): void {
			this.initShowAndCloseMessage();
			this.dispatchEvent(new Event(Event.CLOSE));
		}
		
		public function draw(skinTfMessage: SkinTf): void {
			skinTfMessage = skinTfMessage || new SkinTf();
			skinTfMessage.drawToTf(this.tfMessage, [5, 0][this.isMessageScoreOrLoadSave]);
		}
		
		override public function set width(value: Number): void {
			this.tfMessage.width = value;
		}
		
		override public function get height(): Number {
			var currTextTfMessage: String = this.tfMessage.text;
			this.tfMessage.text = "X\nX";
			var height: Number = this.tfMessage.height;
			this.tfMessage.text = currTextTfMessage;
			return height;
		}
		
		public function destroy(): void {
			TextFieldUtilsDots.stopAddDots(this.tfMessage);
			if (this.timerCloseMessage) this.timerCloseMessage.stop();
			TweenNano.killTweensOf(this);
		}
		
	}
	
}